package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 *	5714
 * http://www.chinastock.com.cn/yhwz/information/index.shtml  中国银河证券
 *
 * @author KK
 *
 */
public class ChinastockNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.chinastock.com.cn/yhwz_about.do\\?.*docId=[0-9]*.*",
		"http://www.chinastock.com.cn/info/SearchInfoDetail.jsp\\?docId=[0-9]*&.*",
		"http://www.chinastock.com.cn/.*/todaypush.do\\?docId=[0-9]*&.*"
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		news.setUrl(url);

		title =doc.select("title").text();
		content=HtmlCleaner.getContentHtml(url ,doc.select("div#Zoom"));

		/*
		 * 作者
		 */
		String authorStr=null;
		authorStr=doc.select("div.d_date").text();
		author=StringUtils.substringBefore(authorStr, "2");


		date=DateUtils.matchDate(doc.select("div.d_date").text());
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(content);
			news.setAuthor(author);
			news.setDate(date);
			news.setType(1);
		return news;
	}
	public static void main(String[] args) {
		String url="http://www.chinastock.com.cn/yhwz/todaypush/todaypush.do?docId=209539246&methodCall=getPZKDdetailInfo&type=2014";
		ChinastockNewsAnalyse analyse=new ChinastockNewsAnalyse();
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
